package com.wk.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wk.common.ReturnObject;
import com.wk.domain.*;
import com.wk.service.FcEstateService;
import com.wk.service.FcUnitService;
import com.wk.service.TblCompanyService;
import com.wk.vo.CellMessage;
import com.wk.vo.UnitMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/estate")
public class EstateController {

    @Autowired
    private FcEstateService  estateService;

    @Autowired
    private TblCompanyService companyService;

    @Autowired
    private FcUnitService unitService;

    @RequestMapping("selectCompany")
    public String selectCompany(){
        LambdaQueryWrapper<TblCompany> wrapper = new LambdaQueryWrapper<TblCompany>().select(TblCompany::getId,TblCompany::getCompanyFullName);
        List<TblCompany> list = companyService.list(wrapper);
        return JSON.toJSONString(new ReturnObject(list));
    }

    /**
     * 判断住宅编码是否存在，如果不存在则添加住宅信息
     * @param fcEstate
     * @return
     */
    @RequestMapping("insertEstate")
    public String insertEstate(FcEstate fcEstate){
        Integer result = estateService.insertEstate(fcEstate);
        if (result == 0) {
            return JSON.toJSONString(new ReturnObject("住宅编码已存在",0));
        }else{
            return JSON.toJSONString(new ReturnObject("住宅信息添加成功",1));
        }
    }

    @RequestMapping("selectBuilding")
    public String selectBuilding(Integer buildingNumber,String estateCode){
        List<FcBuilding> list = estateService.selectBuilding(buildingNumber, estateCode);
        return JSON.toJSONString(new ReturnObject(list));
    }

    @RequestMapping("updateBuilding")
    public String updateBuilding(FcBuilding building){
        if (estateService.updateBuildingById(building)) {
            return JSON.toJSONString(new ReturnObject("更新成功",1));
        }else{
            return JSON.toJSONString(new ReturnObject("更新成功",0));
        }
    }

    @RequestMapping("selectUnit")
    public String selectUnit(@RequestBody UnitMessage[] unitMessages){
        List<FcUnit> units = new ArrayList<>();
        for (UnitMessage unitMessage : unitMessages) {
            units.addAll(estateService.selectUnit(unitMessage));
        }
        return JSON.toJSONString(new ReturnObject(units));
    }


    @RequestMapping("updateUnit")
    public String updateUnit(FcUnit unit){
        if (unitService.updateById(unit)) {
            return JSON.toJSONString(new ReturnObject("更新成功",1));
        }else{
            return JSON.toJSONString(new ReturnObject("更新成功",0));
        }
    }


    @RequestMapping("insertCell")
    public String insertCell(@RequestBody CellMessage[] cellMessages){
        List<FcCell> list = estateService.insertCell(cellMessages);
        return JSON.toJSONString(new ReturnObject(list));
    }

    @RequestMapping("selectBuildingByEstate")
    public String selectBuildingByEstate(String estateCode){
        List<FcBuilding> list = estateService.selectBuildingByEstate(estateCode);
        return JSON.toJSONString(new ReturnObject(list));
    }

    @RequestMapping("selectUnitByBuildingCode")
    public String selectUnitByBuildingCode(String buildingCode){
        List<FcUnit> list = estateService.selectUnitByBuildingCode(buildingCode);
        return JSON.toJSONString(new ReturnObject(list));
    }

    @RequestMapping("selectCellByUnitCode")
    public String selectCellByUnitCode(String unitCode){
        List<FcCell> list = estateService.selectCellByUnitCode(unitCode);
        return JSON.toJSONString(new ReturnObject(list));
    }

    @RequestMapping("selectEstate")
    public String selectEstate(String companyId){
        List<FcEstate> list = estateService.selectEstate(companyId);
        return JSON.toJSONString(new ReturnObject(list));
    }
}

